package com.tianhui.consignortwofei.mvp.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.fgs.common.CommonFragment;
import com.fgs.common.other.CommonEvent;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.mvp.contract.OrderDetailContract;
import com.tianhui.consignortwofei.mvp.model.enty.OrderCondition;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailInfo;
import com.tianhui.consignortwofei.mvp.presenter.OrderDetailPresenter;
import com.tianhui.consignortwofei.mvp.ui.adapter.DetailAdapter;
import com.tianhui.consignortwofei.other.EventWhat;
import com.tianhui.consignortwofei.pattern.orderOprate.IOrderOperate;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchDetailStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.DispatchStatusDingJin;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.FreightStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.InquirySourceStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.PaymentStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.SourceStatus;
import com.tianhui.consignortwofei.pattern.orderStatus.status.impl.SourceStatusDingJin;

import org.yczbj.ycrefreshviewlib.inter.OnItemChildClickListener;
import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.util.List;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2020/8/28
 * @description
 **/
public class OrderDetailActivity extends BaseActivity<OrderDetailContract.View, OrderDetailContract.Presenter> implements OrderDetailContract.View {

    @BindView(R.id.activity_order_detail_recyclerView)
    YCRefreshView mRecyclerView;

    @BindView(R.id.activity_order_detail_actionImageView)
    ImageView mActionImageView;
    private DetailAdapter mAdapter;
    private OrderDetailPresenter mPresenter;
    private IOrderStatus mIOrderStatus;
    private OrderInfo mOrderInfo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    public OrderDetailContract.Presenter createPresenter() {
        mPresenter = new OrderDetailPresenter(this);
        return mPresenter;
    }

    @Override
    public OrderDetailContract.View createView() {
        return this;
    }


    @Override
    public void init() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new DetailAdapter(this);
        mRecyclerView.setAdapter(mAdapter);


        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
            if (mOrderInfo != null) {
                int orderType = bundle.getInt(KeyValue.KEY_TYPE);
                IOrderStatus iOrderStatus = createOrderStatus(orderType);
                if (iOrderStatus != null) {
                    requestOrderDetail();
                } else {
                    showEmptyView();
                }
            } else {
                showEmptyView();
            }
        }


        initListener();
    }

    private void requestOrderDetail() {
        if (mOrderInfo != null && mIOrderStatus != null) {
            OrderCondition orderCondition = new OrderCondition();
            orderCondition.dispatchNo = mOrderInfo.dispatchno;
            orderCondition.billNo = mOrderInfo.billno;
            orderCondition.orderId = mOrderInfo.id;
            getPresenter().requestOrderDetail(mIOrderStatus.getOrderDetailObservable(orderCondition), true, mIOrderStatus.getOrderStatusType());
        }

    }

    private void initListener() {
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {


                switch (view.getId()) {
                    case R.id.item_detail_operateTextView:
                        Object object = mAdapter.getAllData().get(position);
                        if (object instanceof DetailInfo) {
                            DetailInfo detailInfo = (DetailInfo) object;
                            if (detailInfo.iOrderOperate != null) {
                                OrderOperateInfo orderOperateInfo = new OrderOperateInfo();
                                orderOperateInfo.orderInfo = mOrderInfo;
                                detailInfo.iOrderOperate.orderOperate(OrderDetailActivity.this, orderOperateInfo);
                            }
                        }

                        break;
                }
            }
        });

        mAdapter.setOnDetailOperateItemClickListener(new DetailAdapter.OnDetailOperateItemClickListener() {
            @Override
            public void onOperateClick(int position, OrderOperateInfo orderOperateInfo) {
                if (orderOperateInfo != null) {
                    IOrderOperate iOrderOperate = orderOperateInfo.iOrderOperate;
                    if (iOrderOperate != null) {
                        iOrderOperate.orderOperate(OrderDetailActivity.this, orderOperateInfo);
                    }
                }
            }
        });

        mActionImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIOrderStatus != null) {
                    Bundle bundle = new Bundle();
                    bundle.putInt(KeyValue.KEY_TYPE, mIOrderStatus.getOrderStatusType());
                    bundle.putParcelable(KeyValue.KEY_ORDER_INFO, mOrderInfo);
                    openActivity(OrderOperateActivity.class, bundle);
                }
            }
        });
    }

    private IOrderStatus createOrderStatus(int type) {

        switch (type) {
            case OrderType.TYPE_SOURCE:
                mIOrderStatus = new SourceStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("货源单详情");
                return mIOrderStatus;
            case OrderType.TYPE_SOURCE_DINGJIN:
                mIOrderStatus = new SourceStatusDingJin();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("货源单详情");
                return mIOrderStatus;
            case OrderType.TYPE_INQUIRY:
                mIOrderStatus = new InquirySourceStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("询价单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH:
                mIOrderStatus = new DispatchStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("调度单详情");
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH_DINGJIN:
                mIOrderStatus = new DispatchStatusDingJin();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("调度单详情");
                return mIOrderStatus;

            case OrderType.TYPE_FREIGHT:
                mIOrderStatus = new FreightStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("运费计算");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_PAYMENT:
                mIOrderStatus = new PaymentStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("订单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
            case OrderType.TYPE_DISPATCH_DETAIL:
                mIOrderStatus = new DispatchDetailStatus();
                mPresenter.setOrderStatus(mIOrderStatus);
                getToolbarStatus().setTitle("订单详情");
                showFloatActionButton(false);
                return mIOrderStatus;
        }
        return null;
    }

    private void showFloatActionButton(boolean isShow) {
        if (isShow) {
            mActionImageView.setVisibility(View.VISIBLE);
        } else {
            mActionImageView.setVisibility(View.GONE);
        }
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }


    @Override
    public void setOrderInfo(OrderInfo orderInfo) {
        if (orderInfo != null)
            mOrderInfo = orderInfo;
    }

    @Override
    public void showOrderDetailList(List list) {

        if (list != null && list.size() > 0) {
            mAdapter.clear();
            mAdapter.addAll(list);
        } else {
            showEmptyView();
        }
    }

    @Override
    public void showEmptyView() {
        mRecyclerView.setEmptyView(R.layout.layout_empty_view);
        mRecyclerView.showEmpty();
    }

    @Override
    public void onEvent(CommonEvent event) {
        switch (EventWhat.valueOf(event.getWhat())) {
            case EVENT_REFRESH_ORDER_DETAIL:
                requestOrderDetail();
                break;
            case EVENT_ACTIVITY_FINISH:
                finish();
                break;
        }
    }
}
